package my.learn.common.web.core.utils;

import lombok.extern.slf4j.Slf4j;
import my.learn.common.web.core.CamelAndUnderLineConverter;
import my.learn.common.web.core.NormalUsedString;

import java.lang.reflect.Field;

/**
 * 类转换工具
 * 将实体抽象成接口
 * 将枚举抽象成接口
 */
@Slf4j
public final class ClassSwitchUtils {

    /**
     * 将实体抽象成接口
     *
     * @param entityClass 实体
     */
    public static void entitySwitchInterface(Class<?> entityClass) {

        StringBuilder stringBuilder = new StringBuilder();

        stringBuilder.append(NormalUsedString.NEWLINE);

        String simpleName = entityClass.getSimpleName();
        stringBuilder.append(String.format("public interface %s {", simpleName));
        stringBuilder.append(NormalUsedString.NEWLINE);
        for (Field declaredField : entityClass.getDeclaredFields()) {
            Class<?> type = declaredField.getType();
            stringBuilder.append(String.format("%s get%s();", type.getSimpleName(), CamelAndUnderLineConverter.capitalizeFirstLetter(declaredField.getName())));
            stringBuilder.append(NormalUsedString.NEWLINE);
        }

        stringBuilder.append(NormalUsedString.NEWLINE);
        for (Field declaredField : entityClass.getDeclaredFields()) {
            Class<?> type = declaredField.getType();
            stringBuilder.append(String.format("%s set%s();", type.getSimpleName(), CamelAndUnderLineConverter.capitalizeFirstLetter(declaredField.getName())));
            stringBuilder.append(NormalUsedString.NEWLINE);
        }
        stringBuilder.append(NormalUsedString.NEWLINE);
        stringBuilder.append(NormalUsedString.RIGHT_BRACE);
        log.info(stringBuilder.toString());
    }
}
